package com.aaa.backsystem.controller;


import com.aaa.backsystem.entity.*;
import com.aaa.backsystem.service.*;
import com.aaa.backsystem.service.impl.FileUploadService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-08-22
 */
@Controller
@RequestMapping("/wxOption")
@Api(tags = "巡检项目模块接口规范说明")
public class WxOptionController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleServicer;
    @Autowired
    private WxTaskService wxTaskService;

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private WxOptionService wxOptionService;
    @Autowired
    private PetrolStationService petrolStationService;
    /**
     * 微信端登陆
     *
     * @param userName
     * @return
     */
    @ApiOperation(value = "微信小程序端登录接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;巡检人员进行小程序登录")
    @RequestMapping(value = "/WX",method = RequestMethod.GET)
    @ResponseBody
    public Object Wxlogin(String userName, String password) {
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("login_name", userName);
        //通过微信端输入的账户查询对应表中数据
        List<User> users = userService.selectList(wrapper);
        //通过user表查出userid
        Integer value = users.get(0).getUserId().intValue();
        Wrapper<UserRole> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", value);
        //通过userid查出roleid
        List<UserRole> userRoles = userRoleServicer.selectList(wrapper1);
        Integer value1 = userRoles.get(0).getRoleId().intValue();
        if (userName != null && password != null) {
            AuthenticationToken token = new UsernamePasswordToken(userName, password);
            Subject subject = SecurityUtils.getSubject();
            try {
                subject.login(token);
            } catch (AuthenticationException e) {
                e.printStackTrace();
                return false;
            }
            //用户信息传到前端
            if (value1 != 5) {
                //根据roleid是否有登陆微信的权限
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 小程序端显示任务列表
     * @param model
     * @return
     */
    @ApiOperation(value = "查询所有巡检任务列表接口",
            notes = "<span style='color:red;'>描述:</span>&nbsp;用来查询所有巡检任务列表")
    @RequestMapping(value = "/toShowTask",method = RequestMethod.GET)
    @ResponseBody
    public List toShowTask(Model model) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0);
        List problemTypeList = wxTaskService.selectList(wrapper);
        model.addAttribute("taskList", problemTypeList);
        return problemTypeList;
    }

    /**
     * 巡检项下拉框
     * @return
     */
    @ApiOperation(value = "动态获取巡检项列表",
            notes = "<span style='color:red;'>描述:</span>&nbsp;动态获取巡检项列表")
    @RequestMapping(value = "/getOptionName",method = RequestMethod.GET)
    @ResponseBody
    public List<String> getOptionName(){
        Wrapper<WxOption> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("mark");
        List<WxOption> wxOptions = wxOptionService.selectList(wrapper);
        List<String> list = new ArrayList<>();
        for (WxOption wxOption : wxOptions) {
            list.add(wxOption.getMark());
        }
        return list;
    }
    /**
     * 加油站下拉框
     * @return
     */
    @RequestMapping(value = "/getPertolName",method = RequestMethod.GET)
    @ResponseBody
    public List<String> getPertolName(){
        Wrapper<PetrolStation> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("petrol_station_name AS petrolStationName");
        List<PetrolStation> petrolStations = petrolStationService.selectList(wrapper);
        List<String> list = new ArrayList<>();
        for (PetrolStation petrolStation : petrolStations) {
            list.add(petrolStation.getPetrolStationName());
        }
        return list;
    }

}

